#include "platform.h"
#include "SoftI2C.h"
#include "SoftUart.h"



// 指定以下代码不进行优化
// #pragma GCC push_options
// #pragma GCC optimize ("O0")


#define READ_NUM  8

uint8_t txbuf[READ_NUM+8] ;
uint8_t rxbuf[READ_NUM+8];

uint8_t eeprom_bank0  = 0xa0 ;  // 0 ~ 65535 
uint8_t eeprom_bank1  = 0xa2 ;  // 65536 ~ 

#define dev eeprom_bank1 
I2C_t eeprom ;
uint8_t cnt = 0 ;
void main()
{
    P_TXD = 1 ;
    *I2C_BASEADDR = 0xf ; // 仿真时需要
    SoftI2C_Init(&eeprom,I2C_BASEADDR ,     I2C0_PIN_SCL   ,I2C0_PIN_SDA , 10) ;
    TxSend('S') ;TxSend('S') ;
    TxSend('\r') ;TxSend('\n') ;
	while(1){
        for(uint8_t i =0;i<READ_NUM;i++){
            txbuf[i] = cnt + i  ;
        }
        cnt++;

        LED = 0; // 解除写保护
        // Serv_Delayms(100) ;
        if(SoftI2C_M24M01_Write(&eeprom ,dev,0,txbuf,READ_NUM) ) {
            LED = 1; // 写保护
            Serv_Delayms(5) ; // 等待EEPROM数据写入完成  400KH在下芯片内部写数据需要5ms时间
            
            if(SoftI2C_M24M01_Read(&eeprom ,dev,0,rxbuf,READ_NUM) ) {
                for(uint8_t i =0;i<READ_NUM;i++){
                    TxSend(rxbuf[i]) ;
                    rxbuf[i] = 0 ;
                }
            } else {
                TxStr("read error") ;
            }
            TxSend('\r') ;TxSend('\n') ;
        } else {
            TxStr("write error\r\n") ;
        }
        LED = 1; // 写保护

        Serv_Delayms(1000) ;
        
	}
	return ;
}

// #pragma GCC pop_options  // 结束不优化的代码


